//
//  WKStringExtension.swift
//  商家端Swift
//
//  Created by 天下宅 on 16/8/17.
//  Copyright © 2016年 天下宅. All rights reserved.
//

import Foundation
import UIKit

extension String
{
    var md5:String {
        let str = self.cString(using: String.Encoding.utf8)
        let strLen = CUnsignedInt(self.lengthOfBytes(using: String.Encoding.utf8))
        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
        let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
        CC_MD5(str!, strLen, result)
        let hash = NSMutableString()
        for i in 0 ..< digestLen {
            hash.appendFormat("%02x", result[i])
        }
        result.deinitialize()
        return String(format: hash as String)
    }
    
}


//时间戳和时间互转
extension String
{
    static func getNow(WithDateStyle dateStyle:String = "yyyy-MM-dd") -> String
    {
        let dfmatter = DateFormatter()
        dfmatter.dateStyle = DateFormatter.Style.medium
        dfmatter.timeStyle = DateFormatter.Style.short
        dfmatter.dateFormat = dateStyle
        let date = Date.init(timeIntervalSinceNow: 0)
        return dfmatter.string(from: date)
    }
    
    static func getTomorrow(WithDateStyle dateStyle:String = "yyyy-MM-dd") -> String
    {
        let dfmatter = DateFormatter()
        dfmatter.dateStyle = DateFormatter.Style.medium
        dfmatter.timeStyle = DateFormatter.Style.short
        dfmatter.dateFormat = dateStyle
        let date = Date.init(timeIntervalSinceNow: 24 * 60 * 60)
        return dfmatter.string(from: date)
    }
    
    
    //00:00-11:59|12:00:23:59
    static func workTimeToTimeStamps(workTime:String) -> [String]
    {
        
        let timeArr = workTime.components(separatedBy: "|")
        let amS = timeArr[0].components(separatedBy:"-")
        let pmS = timeArr[1].components(separatedBy:"-")
        
        var times = [String]()
        
        times.append(String.stringToTimeStamp(stringTime: amS[0]))
        times.append(String.stringToTimeStamp(stringTime: amS[1]))
        times.append(String.stringToTimeStamp(stringTime: pmS[0]))
        times.append(String.stringToTimeStamp(stringTime: pmS[1]))
        
        return times
    }
    
    
    static func stringToTimeStamp(stringTime:String,dateStyle:String = "HH:mm")->String {
        
        let dfmatter = DateFormatter()
        dfmatter.dateStyle = DateFormatter.Style.medium
        dfmatter.timeStyle = DateFormatter.Style.short
        dfmatter.dateFormat = dateStyle
        
        let date = dfmatter.date(from: stringTime)
        
        let dateStamp:TimeInterval = date!.timeIntervalSince1970
        
        let dateSt:UInt64 = UInt64(dateStamp)
        return String(dateSt)
        
    }
    
    //时间戳转时间
    static func timeStampToString(timeStamp:String,dateStyle:String = "yyyy-MM-dd HH:mm")->String {
        let string = NSString(string: timeStamp)
        return self.timeStampToString(timeS:string.doubleValue,dateStyle: dateStyle)
    }
    
    
    static func timeStampToString(timeS:Double,dateStyle:String = "yyyy-MM-dd HH:mm")->String {
        
        let dfmatter = DateFormatter()
        dfmatter.dateFormat=dateStyle
        let date = NSDate(timeIntervalSince1970: timeS / 1000)
        return dfmatter.string(from: date as Date)
    }
    
    //object 转字符串
    static func anyObjectToJSONStr(obj:AnyObject) -> NSString
    {
        let data =  try! JSONSerialization.data(withJSONObject: obj, options: JSONSerialization.WritingOptions.prettyPrinted)
        let JSONStr = NSString(data: data,encoding: String.Encoding.utf8.rawValue)
        return JSONStr!
        
    }
    
    
    /**
     *  获取字符串的宽度和高度
     *
     *  @param text:NSString
     *  @param font:UIFont
     *
     *  @return CGRect
     */
    static func getTextRectSize(text:NSString,font:UIFont,size:CGSize) -> CGRect {
        let attributes = [NSFontAttributeName: font]
        let option = NSStringDrawingOptions.usesLineFragmentOrigin
        let rect:CGRect = text.boundingRect(with: size, options: option, attributes: attributes, context: nil)
        return rect;
    }
    
    //计算字符串正常宽度，中文字符为2 英文字符为1
    
    func countLength() -> Int
    {
        var length = 0
        for char in self.characters {
            // 判断是否中文，是中文+2 ，不是+1
            length += "\(char)".lengthOfBytes(using: String.Encoding.utf8) == 3 ? 2 : 1
        }
        return length
    }
}
